package com.xiaoshuidi.cloud.module.member.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoshuidi.cloud.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.xiaoshuidi.cloud.module.member.controller.admin.topics.vo.MemberTopicsPostListVo;
import com.xiaoshuidi.cloud.module.member.controller.admin.topics.vo.TopicPointVO;
import com.xiaoshuidi.cloud.module.member.controller.admin.topics.vo.TopicsPostDetailVO;
import com.xiaoshuidi.cloud.module.member.controller.admin.topics.vo.TopicsPostPageVO;
import com.xiaoshuidi.cloud.module.member.controller.admin.topics.vo.TopicsPostUpdateStateVO;
import com.xiaoshuidi.cloud.module.member.controller.app.topics.vo.AppTopicsPostPageVO;
import com.xiaoshuidi.cloud.module.member.controller.app.topics.vo.MemberTopicsPostCreateVO;
import com.xiaoshuidi.cloud.module.member.enums.point.PointRecordEnums;
import com.xiaoshuidi.cloud.module.member.enums.topics.TopicsCommentReviewStatusEnum;
import com.xiaoshuidi.cloud.module.member.mapper.topics.MemberTopicsPostMapper;
import com.xiaoshuidi.cloud.module.member.pojo.topics.MemberTopicsPost;
import com.xiaoshuidi.cloud.module.member.service.point.MemberPointsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Repository
@RequiredArgsConstructor
@Slf4j
public class MemberTopicsPostRepository extends ServiceImpl<MemberTopicsPostMapper, MemberTopicsPost> {
    private final MemberTopicsPostMapper mapper;

    private final MemberPointsService memberPointsService;

    public Map<Long, List<MemberTopicsPost>> getRecordsMapByTopicIds(List<Long> ids) {
        LambdaQueryWrapper<MemberTopicsPost> wrapper = new LambdaQueryWrapper<MemberTopicsPost>()
                .in(MemberTopicsPost::getTopicId, ids);
        List<MemberTopicsPost> list = mapper.selectList(wrapper);
        Map<Long, List<MemberTopicsPost>> collect = list.stream().collect(Collectors.groupingBy(t -> t.getTopicId()));
        return collect;
    }

    public Boolean create(MemberTopicsPostCreateVO createReqVO) {
        MemberTopicsPost memberTopicsPost = new MemberTopicsPost();
        BeanUtils.copyProperties(createReqVO, memberTopicsPost);
        memberTopicsPost.setReviewStatus(TopicsCommentReviewStatusEnum.TO_BE_REVIEW.getStatus());
        mapper.insert(memberTopicsPost);
        return true;
    }

    public IPage<MemberTopicsPostListVo> getPage(TopicsPostPageVO pageVO) {
        return mapper.getPage(new Page<MemberTopicsPost>(pageVO.getPageNo(), pageVO.getPageSize()), pageVO);
    }

    @Transactional
    public Boolean updateStatus(TopicsPostUpdateStateVO updateStateVO) {
        MemberTopicsPost memberTopicsPost = new MemberTopicsPost();
        //操作1
        if (ObjectUtils.isNotEmpty(updateStateVO.getFlag()) && updateStateVO.getFlag().intValue() == 1) {
            if (ObjectUtils.isNotEmpty(updateStateVO.getValue()) && updateStateVO.getValue()) {
                memberTopicsPost.setRecommend(true);
            } else if (ObjectUtils.isNotEmpty(updateStateVO.getValue()) && !updateStateVO.getValue()) {
                memberTopicsPost.setRecommend(false);
            }
        }
        //操作2
        if (ObjectUtils.isNotEmpty(updateStateVO.getFlag()) && updateStateVO.getFlag().intValue() == 2) {
            if (ObjectUtils.isNotEmpty(updateStateVO.getValue()) && !updateStateVO.getValue()) {
                memberTopicsPost.setHide(false);
            } else if (ObjectUtils.isNotEmpty(updateStateVO.getValue()) && updateStateVO.getValue()) {
                memberTopicsPost.setHide(true);
            }
        }

        //操作3 审核通过并显示，审核拒绝并隐藏
        if (ObjectUtils.isNotEmpty(updateStateVO.getFlag()) && updateStateVO.getFlag().intValue() == 3) {
            LocalDateTime reviewTime = LocalDateTime.now();
            if (ObjectUtils.isNotEmpty(updateStateVO.getValue()) && updateStateVO.getValue()) {
                //审核通过并显示
                memberTopicsPost.setReviewStatus(TopicsCommentReviewStatusEnum.REVIEW_PASSED.getStatus());
                memberTopicsPost.setReviewTime(reviewTime);
                memberTopicsPost.setReviewer(updateStateVO.getReviewer());
                memberTopicsPost.setHide(false);
                //赠送积分
                TopicPointVO topicPointVO = mapper.getTopicPoints(updateStateVO.getId());
                //赠送积分
                if (topicPointVO != null && topicPointVO.getBonusPoints() != null) {
                    try {
                        memberPointsService.memberPointsChange(topicPointVO.getUserId(), topicPointVO.getBonusPoints(), PointRecordEnums.TOPIC, "发帖奖励积分");
                    } catch (Exception e) {
                        log.error("用户发帖奖励积分异常", e.getMessage());
                    }
                }

            } else if (ObjectUtils.isNotEmpty(updateStateVO.getValue()) && !updateStateVO.getValue()) {
                memberTopicsPost.setReviewStatus(TopicsCommentReviewStatusEnum.REVIEW_FAILED.getStatus());
                memberTopicsPost.setReviewTime(reviewTime);
                memberTopicsPost.setReviewer(updateStateVO.getReviewer());
                memberTopicsPost.setHide(true);
            }
        }

        memberTopicsPost.setId(updateStateVO.getId());
        mapper.updateById(memberTopicsPost);
        return true;
    }

    public IPage<TopicsPostDetailVO> getAppPage(AppTopicsPostPageVO pageVO) {
        return mapper.getAppPage(new Page<MemberTopicsPost>(pageVO.getPageNo(), pageVO.getPageSize()), pageVO);
    }

    public Boolean addLike(Long id) {
        mapper.addLike(id);
        return true;
    }

    public List<MemberTopicsPost> getTopicsByUserId(Long userId) {
        LambdaQueryWrapperX<MemberTopicsPost> wrapperX = new LambdaQueryWrapperX<MemberTopicsPost>()
                .eqIfPresent(MemberTopicsPost::getCreator, userId.toString());
        return mapper.selectList(wrapperX);
    }

    public List<MemberTopicsPostListVo> exportTopicsPostList(TopicsPostPageVO form) {
        return mapper.exportTopicsPostList(form);
    }

    public int getTopicThumbs(Long id) {
        LambdaQueryWrapperX<MemberTopicsPost> wrapperX = new LambdaQueryWrapperX<MemberTopicsPost>()
                .eqIfPresent(MemberTopicsPost::getTopicId, id);
        List<MemberTopicsPost> list = mapper.selectList(wrapperX);
        if (list != null && list.size() > 0) {
            int sum = list.stream().mapToInt(t -> t.getThumbs()).sum();
            return sum;
        }
        return 0;
    }
}
